package com.example.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.Result;
import com.example.reggie.domain.AddressBook;
import com.example.reggie.domain.DishFlavor;
import com.example.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/addressBook")
@Slf4j
@RestController
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    // 根据用户id查询地址
    @GetMapping("/list")
    Result<List> addressBookList(HttpServletRequest request){
        Long userId =(Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }

    // 添加地址
    @PostMapping
    Result<String> addressBookAdd(@RequestBody AddressBook addressBook,HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return Result.success("添加成功");
    }

    // 设为默认地址
    @PutMapping("/default")
    Result<String> addressBookDefault(HttpServletRequest request,@RequestBody AddressBook address){
        Long userId = (Long)request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(0);
        queryWrapper.eq(AddressBook::getUserId,userId);
        addressBookService.update(addressBook,queryWrapper);
        address.setIsDefault(1);
        addressBookService.updateById(address);
        return Result.success("设置成功");
    }

    // 根据id查询地址信息
    @GetMapping("/{id}")
    Result<AddressBook> addressBookGetById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    // 修改地址
    @PutMapping
    Result<String> addressBookUpdate(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return Result.success("修改成功");
    }

    // 删除地址
    @DeleteMapping
    Result<String> addresBookDelete(Long[] ids){
        addressBookService.removeByIds(Arrays.asList(ids));
        return Result.success("删除成功");
    }

    // 获取当前用户的默认地址
    @GetMapping("/default")
    Result<AddressBook> addressBookGetDefault(HttpServletRequest request){
        Long userId = (Long)request.getSession().getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook defaultAddress = addressBookService.getOne(queryWrapper);
        return Result.success(defaultAddress);
    }

}
